package com.xcf.controller.business.common;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xcf.controller.base.BaseController;
import com.xcf.service.business.common.goodsMaintain.GoodsMaintainService;
import com.xcf.util.PageData;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
@RequestMapping(value = "/goods")
public class GoodsMaintainController extends BaseController{
	
	@Resource
	private GoodsMaintainService goodsmaintainservice;
	String menuUrl="goods/maintain";//菜单地址(权限用) 
	
	@RequestMapping(value="/maintain")
	public ModelAndView maintain(){  
		 ModelAndView mv = new ModelAndView();
	try {
		
	      PageData pd = new PageData(); 
	      pd = this.getPageData();
		 List<PageData> list = goodsmaintainservice.seach(pd);
		  mv.addObject("list", list);
	       mv.setViewName("business/common/goodsMaintain/goods_maintain");
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
    
       return mv;
		
		
	}
	
	/**
	 * 保存或更新数据
	 */
	@RequestMapping(value="/editMaintain")
	@ResponseBody
	public String editMaintain()throws Exception{
		PageData pd = new PageData();
		pd = this.getPageData();
		String data = pd.getString("dataJsonStr");
		JSONArray items = JSONArray.fromObject(data);
		for(int i=0,size=items.size();i<size;i++){
			JSONObject item = items.getJSONObject(i);
			String id = item.getString("id").trim();
			String goods_src_code = item.getString("goods_src_code");
			String goods_src_name = item.getString("goods_src_name");
			int ord_num = Integer.parseInt(item.getString("ord_num"));
			String enable = item.getString("enable");
			PageData pdq = new PageData();
			pdq.put("id", id);
			pdq.put("goods_type_code", goods_src_code);
			pdq.put("goods_type_name",goods_src_name);
			pdq.put("ord_num", ord_num);
			pdq.put("enable", enable);
			if(id==null||id.equals("on")){
				goodsmaintainservice.saveInfo(pdq);
			}else{
				goodsmaintainservice.editInfo(pdq);
			}
			
		}
		return null;
		
	}
	
	//批量删除
		@RequestMapping(value="/deleteInfo")
		@ResponseBody
		public String deleteinfo(){
			PageData pd = new PageData();
			pd = this.getPageData();
				try {
					
					String data = pd.getString("dataJsonStr");

					JSONArray items = JSONArray.fromObject(data);
					for(int i=0,size=items.size();i<size;i++){
						JSONObject item = items.getJSONObject(i);
						String id = item.getString("id").trim();
						PageData pdq = new PageData();
						pdq.put("id", id);
						goodsmaintainservice.deleteU(pdq);
					}
				} catch (Exception e) {
					
					e.printStackTrace();
				}
			
			return null;
			

			
		}
}
